import 'package:flutter/material.dart';
import 'package:flutter_module/beans/data_layer_item_bean.dart';
import 'package:flutter_module/map/map_manager.dart';
import 'package:flutter_module/map/ni_map_controller.dart';
import 'package:flutter_module/map/ni_types.dart';
import 'package:flutter_module/viewModels/drawer_manager_viewmodel.dart';
import 'package:flutter_module/views/drawer_pages/single_selection_page.dart';
import 'package:flutter_module/views/fragments/layer_item_type_list_fragment.dart';
import 'package:flutter_module/views/mapFragmentWidgets/map_title_bar.dart';
import 'package:provider/provider.dart';
import 'package:provider/src/provider.dart';

import 'base_page.dart';

///展示数据编辑页面
class EditDataPage extends BasePage {
  final Map<String, dynamic> _valueControllerMap = {};
  DrawerManagerViewModel? _read;

  @override
  List<Widget> createWidgets(BuildContext context) {
    _read ??= context.read<DrawerManagerViewModel>();
    List<Widget> widgets = [];
    widgets.add(ElevatedButton(
      onPressed: () {
        _read!.addPoint();
      },
      child: const Text("添加点"),
    ));

    for (DataLayerItem item in _read!.curDataLayer!.itemList) {
      widgets.add(_createWidgetItem(item));
    }
    return widgets;
  }

  ///创建图层子项字段
  Widget _createWidgetItem(DataLayerItem item) {
    switch (item.type) {
      case DataLayerItemType.dataLayerItemTypeInput:
        TextEditingController controller = TextEditingController();
        controller.text = "${item.value}";
        _valueControllerMap.putIfAbsent(item.key, () => controller);
        controller.addListener(() {
          item.value = controller.text;
        });
        return TextField(
          maxLines: 2,
          controller: controller,
          decoration: InputDecoration(
            labelText: '${item.title}： ',
            labelStyle: const TextStyle(color: Colors.red),
            icon: const Icon(Icons.description),
          ),
        );
      case DataLayerItemType.dataLayerItemTypeSingleSelection:

        return Card(
          child: Selector<DrawerManagerViewModel, dynamic>(
            selector: (context, provider) => item.value,
            builder: (context, value, child) {

              return ListTile(
                leading: LayerItemConstant.getTypeIcon(
                    DataLayerItemType.dataLayerItemTypeSingleSelection),
                title: Text(item.title),
                subtitle: Text(item.getSelectOptionTitle(item.value)),
                trailing: const Icon(Icons.arrow_forward_ios),
                onTap: () {
                  _read!.curItem = item;
                  _read!.addPage(SingleSelectionPage());
                },
              );
            },
            child: ListTile(
              leading: LayerItemConstant.getTypeIcon(
                  DataLayerItemType.dataLayerItemTypeSingleSelection),
              title: Text(item.title),
              subtitle: Text(item.getSelectOptionTitle(item.value)),
              trailing: const Icon(Icons.arrow_forward_ios),
              onTap: () {
                _read!.curItem = item;
                _read!.addPage(SingleSelectionPage());
              },
            ),
          ),
        );
    }
    return Container();
  }

  @override
  void dispose() {
    _valueControllerMap.values.map((e) {
      if (e is TextEditingController) {
        e.dispose();
      }
    });
    _valueControllerMap.clear();

    if (_read != null && _read!.curDataLayer != null) {
      switch (_read!.curDataLayer!.geometryType) {
        case GeometryType.geometryTypePoint:
          MapManager.instance.removeMarker();
          break;
        case GeometryType.geometryTypeLine:
          MapManager.instance.mapController.cleanDrawLine();
          break;
        case GeometryType.geometryTypePolygon:
          MapManager.instance.mapController.cleanDrawPolygon();
          break;
      }
    }
  }

  @override
  MapTitleBarAdapter createMapTitleBarAdapter(BuildContext context) {
    _read ??= context.read<DrawerManagerViewModel>();
    return MapTitleBarAdapter(
        leading: BackButton(
          color: Colors.black,
          onPressed: () {
            _read!.backPage();
          },
        ),
        title: _read!.curDataLayer!.layerName,
        actions: [
          TextButton(
            onPressed: () {
              _read!.saveData();
            },
            child: const Text('保存'),
          ),
        ]);
  }

  @override
  void update() {
    removeAllItem();
    _valueControllerMap.values.map((e) {
      if (e is TextEditingController) {
        e.dispose();
      }
    });
    _valueControllerMap.clear();

    addItem(ElevatedButton(
      onPressed: () {
        _read!.addPoint();
      },
      child: const Text("添加点"),
    ));

    for (DataLayerItem item in _read!.curDataLayer!.itemList) {
      addItem(_createWidgetItem(item));
    }
  }
}
